<template>
	<view class="container p20">
		<template v-if="list.length>0">
			<view class="listItem" v-for="(item,index) in list" :key="index" @click.stop="path('/pages/rich/index?url='+ item.url+'&title='+item.title+'&img='+item.thumbnail_url)" >
				<view class="listImg">
					<image :src="item.thumbnail_url" mode=""></image>
				</view>
				<view class="listInfo">
					<view class="listName">{{item.title}}</view>
					<image  class="collection" @click.stop="clickTap(item)" src="../../static/images/collection.png" mode=""></image>
				</view>
			</view>
		</template>
		<view class="nodata" v-else>
			<image src="../../static/images/nodata.png" mode=""></image>
			<view class="c9">{{type == 'public'?'暂无咨询':'暂无收藏'}}</view>
		</view>
	</view>
</template>

<script>
	import  base64 from '../../core/base64-c.js'
	export default {
		name:"",
		data() {
			return {
				page_index:0,
				rows_per_page:10,
				list:[],
				type:""
			}
		},
		onLoad(o) {
			this.type = o.type;
			if(o.type == 'public'){
				this.getNewsList();
			}else{
				this.getcolList();
			}
		},
		onReachBottom() {
			this.page_index++;
			if(o.type == 'public'){
				this.getNewsList();
			}
		},
		methods: {
			clickTap(item){
				console.log(11);
				if(this.type == 'public'){
					this.collectionTap(item);
				}else{
					this.delected(item);
				}
			},
			// 收藏文章
			collectionTap(item){
				let _this = this;
				_this.$utils.request({
					url:'/patient/bookmarks/',
					method:"POST",
					data:{
						thumbnail_url:item.thumbnail_url,
						title:item.title,
						url:item.url
					}
				}).then(res=>{
					uni.showToast({
						title:"收藏成功"
					})
				}).catch(err=>{
					uni.showToast({
						title:"已收藏过了",
						icon:"error"
					})
				})
			},
			// 取消收藏
			delected(item){
				let _this = this;
				_this.$utils.request({
					url:'/patient/bookmarks/',
					method:"DELETE",
					data:{
						url_array:[item.url],
					}
				}).then(res=>{
					uni.showToast({
						title:"取消收藏"
					})
					_this.getcolList()
				})
			},
			// 获取收藏列表
			getcolList(){
				let _this = this;
				_this.$utils.request({
					url:"/patient/bookmarks/",
				}).then(res=>{
					_this.list  = res.data
				})
			},
			// 获取文章列表
			getNewsList(){
				let _this = this;
				_this.$utils.request({
					url:"/patient/news/",
					data:{
						page_index:_this.page_index,
						rows_per_page:_this.rows_per_page
					}
				}).then(res=>{
					if(res.data.news.length<1){
						uni.showToast({
							title:"已经到底了",
							icon:"none"
						})
					}else{
						res.data.news.forEach(item=>{
							item.title = base64.decode(item.title) 
						})
						_this.list = [].concat(_this.list,res.data.news);
					}
				})
			},
			// 跳转
			path(url){
				if(!this.$utils.isLogin()){
					return false;
				}
				this.$routeJump.navigateTo(url)
			}
		}
	}
	
</script>

<style lang="scss" scoped>
	.listItem{
		display: flex;
		margin-bottom: 20rpx;
		.listImg{
			width: 240rpx;
			height: 160rpx;
			border-radius: 14rpx;
			overflow: hidden;
			margin-right: 20rpx;
		}
		.listInfo{
			width: 430rpx;
			.listName{
				height: 100rpx;
				font-size: 32rpx;
				line-height: 54rpx;
				display: -webkit-box;
				-webkit-box-orient: vertical;
				-webkit-line-clamp: 2;
				overflow: hidden
			}
			.collection{
				width: 46rpx;
				height: 46rpx;
				float: right;
			}
		}
	}
</style>
